<?php

namespace app\search\controller;

use app\lib\enum\Status;
use think\Exception;
use think\Log;
use think\Request;

class Index extends BaseController
{
    //首页
    public function index(){
        //加载地区，年份
        $yearList=model("PyAttr")->where("attr_type","=",Status::year)
            ->order("attr_order","asc")
            ->select();
        $locationList=model("PyAttr")->where("attr_type","=",Status::location)
            ->order("attr_order","asc")
            ->select();
        $this->assign("yearList",$yearList);
        $this->assign("locationList",$locationList);


        //付费金额
        $web_search_fee=cache("webInfo")['web_search_fee'];
        $this->assign("web_search_fee",10);


        //将免费搜索的数量显示到页面上
        $freeNum=model("PySearchLog")->getFreeNum();
        $this->assign("freeNum",$freeNum);
        return view();
    }

    //判断是否存爱
    public function checkIsset(){
//        year=2&location=1&slo_school_code=&slo_school_name=&slo_profession_code=&slo_profession_name=&slo_grade_min=&slo_grade_max=&slo_position_min=&slo_position_max=
        $data=Request::instance()->param();
        $this->uniteResponse(model("PyAdmissionInformation")->checkIsset($data),"信息存在");
    }

    //判断是否搜索过
    //如果用户能够不付费直接下载：success()
    //其他：error()
    public function checkSearch(){
        $data=Request::instance()->param();
        $result=model("PySearchLog")->checkSearch($data);
        //如果是数组数据，说明可以直接进行查询
        if(is_array($result)){
            $baseUrl=url("search/Index/search");
            $this->success("在该条件下可直接查询",$baseUrl."?slo_wechat_code=".$result['slo_wechat_code']);
        }else{
            $this->error($result);
        }
    }

    //获取支付参数
    public function getPayParams(){
        $data=Request::instance()->param();
        try{
            $result=model("PySearchLog")->getPay($data);
        }catch (Exception $exception){
            $this->error("异常");
            return false;
        }
        if(is_array($result)){
            $url=url("search/Index/search");
            $this->success("参数获取成功",$url."?slo_wechat_code=".$result['slo_wechat_code'],$result['data'],$result['slo_wechat_code']);
        }else{
            $this->error($result);
        }
    }

    //检验标识文件是否存在
    public function checkTagsExists(){
        $data=Request::instance()->param();
        //将信息转为大写
        $filePath=config("setting.tagsFilePath").$data['slo_wechat_code'];
        if(file_exists($filePath)){
            //如果标识文件存在，说明尚未收到微信支付成功的回调（收到回调会删除标识文件），则未支付成功
            $this->success();
        }else{
            $this->error();
        }
    }


    //记录查看页面
    public function search(){
        $slo_wechat_code=input("get.slo_wechat_code");
        if(!$slo_wechat_code){
            //为传入订单号，非法请求
            $this->error("非法请求");
        }
        if(request()->isAjax()){
            $checkResult=model("PySearchLog")->checkWechatCode($slo_wechat_code);
            if(!is_array($checkResult)){
                $this->error($checkResult);
            }

            $page=input("post.page");
            $limit=input("post.limit");
            $data=$this->assembleCondition($slo_wechat_code);
            $condition=$data['condition'];


            $count=getCount("PyAdmissionInformation",$condition);
            $infoList=model("PyAdmissionInformation")
                ->with("bindLocation,bindYear")
                ->where($condition)
                ->order("ain_grade","desc")
                ->order("ain_position","asc")
                ->order("ain_school_code","asc")
                ->page($page,$limit)
                ->select();

            foreach ($infoList as &$value){
                if($value['ain_position']==-1){
                    $value['ain_position']="";
                }
            }
            return json([
                "code"=>1,
                "count"=>$count,
                "data"=>$infoList,
                "page"=>(int)$page,
                "limit"=>(int)$limit,
                "currentNum"=>count($infoList)
            ]) ;
        }





        $checkResult=model("PySearchLog")->checkWechatCode($slo_wechat_code);
        if(!is_array($checkResult)){
            $this->error($checkResult);
            return false;
        }
        try{
            //如果传入的订单号 不存在，就会产生异常
            $conditionData=$this->assembleCondition($slo_wechat_code);
            $condition=$conditionData["condition"];
        }catch (Exception $exception){
            $this->error("数据异常");
            return false;
        }

        $count=getCount("PyAdmissionInformation",$condition);
        $web_search_time=model("CmsWebsite")->getCacheData("web_search_time");
        $this->assign("web_search_time",$web_search_time);
        $this->assign("count",$count);
        $this->assign("slo_wechat_code",$slo_wechat_code);
        $this->assign("limit",config("setting.searchNum"));

        return view();
    }

    //组装查询condition
    //根据前台传入的表单信息返回搜索条件
    public function assembleCondition($wechatCode){
        $searchInfo=model("PySearchLog")->where("slo_wechat_code","=",$wechatCode)->find();
        //组装两个数据，使得searchInfo变量能够符合getCondition()函数的要求
        $searchInfo['location']=$searchInfo['slo_location_id'];
        $searchInfo['year']=$searchInfo['slo_year_id'];

        //返回对信息表的查询条件，用该条件显示具体的数据
        $condition=model("PyAdmissionInformation")->getCondition($searchInfo);
        //同时将 日志记录和搜索条件已数组的形式返回出去
        return [
            "condition"=>$condition,
            "searchInfo"=>$searchInfo
        ];
    }



    //判断是否可以免费查询
    public function checkFreeGet(){
        $data=Request::instance()->param();
        $result=model("PySearchLog")->checkFreeGet($data);
        if(is_array($result)){
            //返回数组，说明用户可以直接花费免费次数来免费搜索
            $url=url("search/Index/search");
            $this->success("",$url."?slo_wechat_code=".$result['slo_wechat_code']);
        }else{
            $this->error($result);
        }
    }

    //判断用户是否有免费搜索的次数
    public function isHasFreeNum(){
        $result=model("PySearchLog")->getFreeNum();
        $result>0?$this->success("","",$result):$this->error();
    }

    //创建免费的查询记录
    public function createFreeLog(){
        $data=Request::instance()->param();
        $result=model("PySearchLog")->createFreeLog($data);
        if(is_array($result)){
            $url=url("search/Index/search")."?slo_wechat_code=".$result['slo_wechat_code'];
            $this->success("记录创建成功",$url);
        }else{
            $this->error($result);
        }

    }

    //显示搜索提示信息
    public function showHint(){
        $web_search_hint=model("CmsWebsite")->getCacheData("web_search_hint");
        $this->assign("web_search_hint",$web_search_hint);
        return view();
    }


}
